<?php
require_once ("./includes/settings.php");

Database::Create(); //Create our instance of the Database object.

class Database
{
	private static $db_name, $host, $user, $pass, $db_type, $con_info, $link, $link_instance, $database_instance;
	
	private function __construct()
	{
		self::$host             = HOST;
		self::$user             = USER;
		self::$pass             = PASS;
		self::$db_name          = DB_NAME;
		self::$db_type          = DB_TYPE; // 0 - MySQL; 1 - MS SQL;
		self::$link_instance    = self::connect();
	}
	
	public function __destruct()
	{
		switch (self::$db_type)
		{
			case "1": // MS SQL
				sqlsrv_close(self::$link_instance);
			break;
			case "0": // My SQL
				mysqli_kill();
				mysqli_close();
			break;
		}
	}
	
	public static function create()
	{
		if(!isset(self::$database_instance))
		{
			$class = __CLASS__;
			self::$database_instance = new $class;
		}
		
		return self::$database_instance;
	}
	
	private static function connect()
	{
		switch (self::$db_type)
		{
			case "1": //MS SQL
				self::ms_sql_connect();
			break;
			case "0": //MY SQL
				self::my_sql_connect();
			break;
		}
		
		return self::$link;
	}
	
	private static function ms_sql_connect()
	{
		self::$con_info = 
		array("UID" => self::$user, 
			  "PWD" => self::$pass, 
			  "Database"=>self::$db_name);
		self::$link = 
		sqlsrv_connect(self::$host, 
					   self::$con_info)
		or die(print_r(sqlsrv_errors(SQLSRV_ERR_ERRORS), true));
		
		return self::$link;	
	}
	
	private static function my_sql_connect()
	{
		self::$link = 
		mysqli_connect(self::$host, 
					   self::$user, 
					   self::$pass)
		or trigger_error(mysql_error(),E_USER_ERROR);
		
		return self::$link;
	}
	
	private static function db_query($sql)
	{
		switch (self::$db_type)
		{
			case "1": //MS SQL
				$res = sqlsrv_query(self::$link_instance, $sql)or die("Error, no results match.");
				$row = sqlsrv_fetch_array($res);
			break;
			case "0": //MY SQL
				$res = mysqli_query(self::$link_instance, $sql)or die("Error, no results match.");
				$row = mysqli_fetch_assoc($res);
			break;
		}
		
		return $row;
	}
	
	public static function select_query($table, array $columns = null, array $where = null, array $order = null)
	{
		//$table   = Validate::sql_validate($table);
		//$columns = Validate::sql_validate($columns);
		//$where   = Validate::sql_validate($where);
		//$order   = Validate::sql_validate($order);
		
		$sql = "SELECT "; //start our query string

		if($columns == null) //$columns was not set, grab data from all columns
		{
			$sql .= "*";
		}
		else
		{ //$columns was set, grab data from only those columns
			$i = 0; 
			foreach($columns as $column)
			{
				$sql .= ($i == count($columns) - 1) 
						? $column
						: $column . ",";
				$i++;
			}
		}
		
		$sql .= " FROM " . $table; //grab data from $table
		
		if(!$where == null)
		{
			$sql .= " WHERE ";

			foreach($where as $column => $value) //grab data where $column = $value(ex: $where = array("id" => 2) = id=2)
			{
				if($column[0] == '&') $sql .= " AND "; //ex: array("id" => "2", "&title" => "Example") equates to WHERE id='2' AND title='Example'
				if($column[0] == '|') $sql .= " OR ";  //ex: array("id" => "2", "|title" => "Example") equates to WHERE id='2' OR title='Example'
				$sql .= $column . " = '" . $value ."'";
			}
		}
		
		if(!$order == null)
		{
			$sql .= " ORDER BY ";
			$i = 0;
			foreach($order as $column) //grab data where $column = $value(ex: $where = array("id" => 2) = id=2)
			{
				$sql .= $column;
				if($column[0] == '!') $sql .= " DESC"; //ex: array("id", "!title") equates to ORDERBY id ASC, title DESC
				else $sql .= " ASC"; 
				$sql .= ($i == count($order) - 1) 
						? ""
						: ",";
			}
		}
		
		return self::db_query($sql);
	}
}
?>